<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Motor Control</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin: 0;
            padding: 0;
            background-color: #f4f4f9;
        }
        .container {
            max-width: 400px;
            margin: 50px auto;
            padding: 20px;
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #333;
        }
        .slider-container {
            margin: 20px 0;
        }
        .slider {
            width: 100%;
        }
        .buttons {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .start {
            background-color: #4CAF50;
            color: white;
        }
        .stop {
            background-color: #f44336;
            color: white;
        }
        .macro-container {
            margin-top: 30px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .macro-content {
            width: 100%;
            height: 100px;
            margin: 10px 0;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            resize: vertical;
        }
        .macro-buttons {
            display: flex;
            justify-content: space-between;
            margin-top: 10px;
            gap: 10px; /* 增加按钮间距 */
        }
        .macro-start {
            background-color: #2196F3;
            color: white;
        }
        .macro-stop {
            background-color: #FF9800;
            color: white;
        }
        .macro-save {
            background-color: #9C27B0;
            color: white;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Motor Control</h1>
        <div class="slider-container">
            <label for="speed">Speed: <span id="speedValue">0</span></label>
            <input type="range" id="speed" class="slider" min="-9000" max="9000" value="0" oninput="updateSpeedValue(this.value)">
        </div>
        <div class="buttons">
            <button class="start" onclick="startMotor()">Start</button>
            <button class="stop" onclick="stopMotor()">Stop</button>
        </div>
        <div class="macro-container">
            <h2>Macro Control</h2>
            <textarea id="macro-content" class="macro-content"></textarea>
            <div class="macro-buttons">
                <button class="macro-start" onclick="startMacro()">Start Macro</button>
                <button class="macro-save" onclick="saveMacro()">Save Macro</button>
                <button class="macro-stop" onclick="stopMacro()">Stop Macro</button>
            </div>
        </div>
    </div>
    <script>
        function updateSpeedValue(value) {
            document.getElementById('speedValue').innerText = value;
        }
        function startMotor() {
            fetch(`/start?speed=${document.getElementById('speed').value}`);
        }
        function stopMotor() {
            fetch('/stop');
        }
        function startMacro() {
            fetch('/macro/start');
        }
        function stopMacro() {
            fetch('/macro/stop');
        }
        // 页面加载时获取宏内容和电机速度
        window.addEventListener('load', function() {
            // 加载宏内容
            fetch('/macro/content')
                .then(response => response.text())
                .then(content => {
                    document.getElementById('macro-content').value = content;
                });
            
            // 加载当前电机速度
            fetch('/motor/speed')
                .then(response => response.json())
                .then(data => {
                    document.getElementById('speed').value = data.speed;
                    document.getElementById('speedValue').innerText = data.speed;
                });
        });

        function saveMacro() {
            const content = document.getElementById('macro-content').value;
            fetch('/macro/save', {
                method: 'POST',
                headers: {
                    'Content-Type': 'text/plain',
                },
                body: content
            }).then(response => {
                if (response.ok) {
                    alert('Macro saved successfully');
                } else {
                    alert('Failed to save macro');
                }
            });
        }
    </script>
</body>
</html>
